home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8185 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing 2-dimensional Array
  5. Date: 15 Feb 1996 16:32:06 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4fvn66$doe@dawn.mmm.com>
  8. References: <4fo6a9$hvt@tali.UCHSC.edu> <311D100B.7908@gsfc.nasa.gov>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Dirk Broer (Dirk.Broer@gsfc.nasa.gov) wrote:
  13. > Jan Ingebrigtsen wrote:
  14. > > 
  15. > > How do I pass a pointer to a 2 dimensional array in function calls in C?
  16. > > 
  17. > > I have tried the following but have had no success:
  18. > > 
  19. > >         main()
  20. > >         {
  21. > >                 double (*TwoDim)[Y] = new double [X][Y];
  22. > >                 foo( TwoDim, X, Y )
  23. > >         }
  24. > > 
  25. > >         void foo( double *TwoDim[], int x, int y )
  26. > >         {
  27. > >                 // process TwoDim here
  28. > >         }
  29. > > 
  30. > > I will get the following error message:
  31. > > 
  32. > >         cannot convert paramter 1 from 'double (__near *)[Y]' to 'double
  33. > > __near*'
  34. > > 
  35.  
  36.  
  37. > A 2 dimensional array is created in C as a pointer to a pointer.  So if 
  38. > you create an NxM array C creats an N array of pointers to other arrays.
  39. > Additionally it creates N arrays with M values in it.  Try defining the 
  40. > function as:
  41.  
  42. This is not correct.  The statement
  43.  
  44.     double (*TwoDim)[Y] = new double[X][Y];
  45.  
  46. allocates X*Y doubles and makes TwoDim point to the first one.  No
  47. pointers are allocated by this call to new.
  48.  
  49. > func( double **TwoDim );
  50.  
  51. This is equivalent to
  52.     func(double* TwoDim[]);
  53.  
  54. > TwoDim can them be accessed normally using TwoDim[x][y];
  55.  
  56. > Creating TwoDim might not be as easy through.
  57.  
  58. It is easy if you do not want to allocate an array of pointers to
  59. double and use it as a 2D array of double.
  60.  
  61. > >                 double (*TwoDim)[Y] = new double [X][Y];
  62.  
  63. > I'm surprised this worked.  The way I read it you are creating XxY doubles 
  64. > and returning a pointer to the first one.  When you try to access it via
  65. > TwoDim[x][y] the compiler first takes the pointer TwoDim and offsets 'x' - 
  66. > this will give you the xth item you've created.  It then takes a further 
  67. > offset of [y] and returns the value at [x+y];  This is clearly wrong.
  68.  
  69. > Am I missing something?
  70.  
  71. The compiler will offset x*Y+y doubles.
  72.  
  73. > double (*TwoDim)[y] is of course an array of y pointers to doubles.
  74.  
  75. > >         void foo( double *TwoDim[], int x, int y )
  76.  
  77. > double *TwoDim[] is a pointer to an array of doubles....
  78.  
  79. > Put brackets around *TwoDim and it should compile - but it still may not 
  80. > work right.  Double check what happens when you do new[x][y];
  81.  
  82. Correct, it will not work right unless you also put a Y in the brackets:
  83.         void foo( double (*TwoDim)[Y], int x, int y )
  84. --
  85. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  86. 3M Company                      phone:  (612) 737-4643
  87. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  88. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  89.     But 3M speaks for me -- I did not write the following line:
  90.  
  91. Opinions expressed herein are my own and may not represent those of 3M.
  92.